<?php

namespace common\service;

use common\exception\LogicException;
use common\model\ModuleModel;
use common\model\TaskModel;
use common\model\VersionModel;
use support\SingletonTrait;
use think\Exception;
// 任务文件服务层
class TaskFileService
{

    use SingletonTrait;

    private $versionModel;
    private $moduleModel;

    public function __construct()
    {
        $this->versionModel = model(VersionModel::class);
        $this->moduleModel = model(ModuleModel::class);
    }

    /**
     * 查找任务最新发布版本
     * @param $param
     *
     * @return array
     */
    public function getLatestPublishedVersionByTaskId($param)
    {
        if (empty($param)) {
            return  [];
        }

        $taskModuleData = module_by_code('task');
        // 查找条件
        $where = [
            'link_id' => $param['task_id'],
            'is_publish' => 'yes',
            'module_id' => $taskModuleData['id']
        ];
        //取最新版本号id
        $latestVersion = $this->versionModel
            ->field('max(id) as id')
            ->where($where)
            ->order('id desc')
            ->group('link_id')
            ->select();
        $latestIds = array_column($latestVersion, 'id');
        $field = [
            "id", "project_id", "number", "link_id as task_id","is_publish",
            "JSON_UNQUOTE(JSON_EXTRACT(json, CONCAT('$.','version_file_work'))) as version_file_work",
            "JSON_UNQUOTE(JSON_EXTRACT(json, CONCAT('$.','version_file_review'))) as version_file_review",
            "JSON_UNQUOTE(JSON_EXTRACT(json, CONCAT('$.','version_file_public'))) as version_file_publish",
        ];
        // 查找任务发布文件最新版本
        $result =  $this->versionModel
            ->field($field)
            ->where(['id' => ['in' , $latestIds]])
            ->select();


        if(empty($result)){
            return [];
        }

		foreach ($result as $key => &$value){
            if (!empty($value['version_file_work'])) {
                $value['version_file_work'] = json_decode($value['version_file_work'], true);
            }
            if(!empty($value['version_file_review'])){
                $value['version_file_review'] = json_decode($value['version_file_review'] , true);
            }
            if(!empty($value['version_file_publish'])){
                $value['version_file_publish'] = json_decode($value['version_file_publish'] , true);
            }
		}
        return $result;
    }

	/**
	 * 查找任务文件
	 * @param $param
	 *
	 * @return void|array
	 */
	public function getUpstreamFileByTask($param)
	{
		if(empty($param)){
			return [];
		}
		$taskModuleData = module_by_code('task');
		// 查找条件
		$where = [
            'link_id' => is_array($param['task_id']) ? ['in', $param['task_id']] : $param['task_id'],
			'is_publish' => 'yes',
			'module_id' => $taskModuleData['id']
		];

		$latestVersion = $this->versionModel
			->field('id')
			->where($where)
			->order('id desc')
			->group('link_id')
			->select();
		$latestIds = array_column($latestVersion, 'id');

		$field = [
			"id", "project_id", "number", "link_id as task_id","is_publish",
			"JSON_UNQUOTE(JSON_EXTRACT(json, CONCAT('$.','version_file_public'))) as version_file_publish",
		];
		// 查找任务文件
		$result =  $this->versionModel
			->field($field)
			->where(['id' => ['in' , $latestIds]])
			->select();
		if(empty($result)){
			return [];
		}

		foreach ($result as $key => &$value){
            if(!empty($value['version_file_publish'])){
                $value['version_file_publish'] = json_decode($value['version_file_publish'] , true);
            }
		}

		return $result;
	}

	/**
	 * 参考文件
	 * @param $param
	 *
	 * @return void|array
	 */
	public function getReferenceFiles($param)
	{
		if(empty($param))
		{
			return [];
		}

        $taskModuleData = module_by_code('task');
        $where = [
            'task.id' => is_array($param['task_id']) ? ['in', $param['task_id']] : $param['task_id'],
            'version.is_publish' => 'yes',
            'version.module_id' => $taskModuleData['id'],
            'task.step_type' => ['in',['standard_meeting','standard_review','standard_make_to_do']]
        ];

        $field = [
            "task.id", "version.project_id", "version.number", "version.link_id as task_id","version.is_publish",
            "JSON_UNQUOTE(JSON_EXTRACT(version.json, CONCAT('$.','version_file_public'))) as version_file_publish",
        ];
		// 查询参考任务数据信息
		$taskModelInfo = model(TaskModel::class)
			->alias('task')
            ->join('version on task.id = version.link_id','left')
            ->field($field)
			->where($where)
			->select();

        if(empty($taskModelInfo)){
           return [];
        }
        foreach ($taskModuleData as $key => &$value){
            if(!empty($value['version_file_publish'])){
                $value['version_file_publish'] = json_decode($value['version_file_publish'] , true);
            }
        }

        return $taskModelInfo;
	}
}
